<?php

namespace App\Http\Controllers\System;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class SupplierController extends Controller
{
    public function index()
    {
        return view('system.supplier',[
            's_pactList'=>DB::table('pact')->where('Type','>',0)->get(),
            'companyList'=>DB::table('mycom')->where(['Type'=>4,'Del'=>1])->get(),
            'sup_lists'=>DB::table('use')->where('Supplier',1)->get()
        ]);
    }

    public function create(Request $request)
    {
        if($request->has('listId'))//条目修改功能
        {
            $insert=array(
                'ComId'=>$request->input('company_id'),
                'Type'=>$request->input('type'),
                'Name'=>trim($request->input('name')),
                'WW'=>$request->input('ww'),
                'QQ'=>$request->input('qq'),
                'Call'=>$request->input('call'),
                'Email'=>$request->input('email'),
                'Mobile'=>$request->input('mobile'),
                'UseId'=>session('user')[0]->id
            );
            if($request->input('Pact_id'))
            {
                $insert['Pactid']=$request->input('Pact_id');
            }
            else
            {
                $insert['Pactid']=0;
            }
            DB::table('use')->where('id',$request->input('listId'))->update($insert);
            return redirect('system/supplier/index')->with('callback', '供应商修改成功');
        }
        else//条目添加功能
        {
            $insert=array(
                'ComId'=>$request->input('company_id'),
                'Type'=>$request->input('type'),
                'Name'=>trim($request->input('name')),
                'WW'=>$request->input('ww'),
                'QQ'=>$request->input('qq'),
                'Call'=>$request->input('call'),
                'Email'=>$request->input('email'),
                'Mobile'=>$request->input('mobile'),
                'UseId'=>session('user')[0]->id,
                'Jurisdiction'=>1,
                'Supplier'=>1
            );
            if($request->input('Pact_id'))
            {
                $insert['Pactid']=$request->input('Pact_id');
            }
            else
            {
                $insert['Pactid']=0;
            }
            DB::table('use')->insert($insert);
            return redirect('system/supplier/index')->with('callback', '供应商添加成功');
        }
    }

    //供应商list展示
    public function show(Request $request)
    {

    }

    public function update(Request $request)
    {
        
    }

    public function destroy(Request $request)
    {
        if($request->input('del')==1)
        {
            DB::table('use')->where('id',$request->input('uid'))->update(['Del'=>0]);
            return 1;
        }
        else
        {
            DB::table('use')->where('id',$request->input('uid'))->update(['Del'=>1]);
            return 1;
        }
    }


    public function test()
    {
        abort(404);
    }




}
